﻿using Slate;
using UniFramework.Battle;
using UniFramework.Skill;
using UnityEngine;

namespace UniFramework.Skill
{
	[System.ComponentModel.Category("Effect")]
	[System.ComponentModel.Description("创建Effect")]
	[Attachable(typeof(SkillTrack))]
	public class EffectClip : SkillBaseClip
	{
		public SKillEffectConfigNode data = new();
		private GameObject _devTempObj;

		protected override void OnEnter()
		{
			base.OnEnter();
			if (_devTempObj != null)
				_devTempObj = GameObject.Instantiate(data.effectPrefab);
			TryEffectFollowActor();
		}

		protected override void OnUpdate(float time, float previousTime)
		{
			base.OnUpdate(time, previousTime);
			TryEffectFollowActor();
		}

		protected override void OnExit()
		{
			if (_devTempObj != null)
				GameObject.DestroyImmediate(_devTempObj);
			data = null;
		}

		private void TryEffectFollowActor()
		{
			if (_devTempObj == null || !_devTempObj.activeSelf) return;
			var goRt = this.actor.transform;
			var rt = _devTempObj.transform;
			rt.position = data.offset + goRt.position;
		}

		public override object GetData()
		{
			data.triggerFrame = LogicFrameConfig.GetFrameCnt(startTime);
			data.endFrame = LogicFrameConfig.GetFrameCnt(endTime);
			return data;
		}

		public override void RefreshTime()
		{
			startTime = LogicFrameConfig.GetRealTime(data.triggerFrame);
			endTime = LogicFrameConfig.GetRealTime(data.endFrame);
		}
	}
}